package zuel.innovation.disley.apply.controller;

import cn.fabrice.common.pojo.BaseResult;
import cn.fabrice.common.pojo.DataResult;
import cn.fabrice.jfinal.annotation.Param;
import cn.fabrice.jfinal.annotation.ValidateParam;
import cn.fabrice.jfinal.ext.cros.interceptor.CrossInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import zuel.innovation.disley.apply.service.ApplyService;
import zuel.innovation.disley.common.module.Apply;
import zuel.innovation.disley.common.module.User;
import zuel.innovation.disley.interceptor.AuthInterceptor;
import zuel.innovation.disley.user.service.UserService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Before(CrossInterceptor.class)
@Path("/apply")
@ValidateParam


public class applyController extends Controller {
    @Inject
    UserService service;
    @Inject
    ApplyService applyService;


    /**
     * 填写报名表
     *
     * @param apName         姓名
     * @param apSex          性别
     * @param apPhone        电话
     * @param apEmail        邮箱
     * @param apGrade        年级
     * @param apInstitute    学院
     * @param apMajor        专业
     * @param apExperience   个人经历
     * @param apIntroduction 个人简介
     * @param apGoal         个人目标
     * @param rId            招新编号
     * @throws ParseException
     */
    @Clear(AuthInterceptor.class)
    @Param(name = "apName", required = true)
    @Param(name = "apSex", required = true)
    @Param(name = "apPhone", required = true)
    @Param(name = "apEmail", required = true)
    @Param(name = "apGrade", required = true)
    @Param(name = "apInstitute", required = true)
    @Param(name = "apMajor", required = true)
    @Param(name = "apExperience", required = true)
    @Param(name = "apIntroduction", required = true)
    @Param(name = "apGoal", required = true)
    @Param(name = "rId", required = true)
    public void addApply(String apName, int apSex, String apPhone, String apEmail, int apGrade, String apInstitute, String apMajor, String apExperience, String apIntroduction, String apGoal, int rId) throws ParseException {
        Apply apply = new Apply();
        apply.setApName(apName);
        apply.setApSex(apSex);
        apply.setApPhone(apPhone);
        apply.setApEmail(apEmail);
        apply.setApGrade(apGrade);
        apply.setApInstitute(apInstitute);
        apply.setApMajor(apMajor);
        apply.setApExperience(apExperience);
        apply.setApIntroduction(apIntroduction);
        apply.setApGoal(apGoal);
        apply.setApPass(0);
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
        Date apCreate = df.parse(df.format(System.currentTimeMillis()));
        apply.setApCreate(apCreate);
        apply.setApDeleted(0);
        apply.setRId(rId);
        renderJson(apply.save() ? BaseResult.ok() : BaseResult.fail());

    }


    /**
     * 管理员分页获取某次招新报名信息
     *
     * @param page     页数
     * @param pageSize 每页信息条数
     * @param rId      招新编号
     */
    @Param(name = "page", required = true)
    @Param(name = "pageSize", required = true)
    @Param(name = "rId", required = true)
    public void getApplyByPages(int page, int pageSize, int rId) {
        Page<Record> applys = Db.paginate(page, pageSize, "select ap_id,ap_phone,ap_name,ap_sex,ap_email,ap_grade,ap_institute,ap_major,ap_pass,ap_deleted ", "from apply where r_id=" + rId);
        renderJson(DataResult.data(applys));

    }

    /**
     * 获取招新总人数
     */
    @Param(name = "rId",required = true)
    public void getSum(int rId){
        Kv cond=Kv.by("rId",rId);
        Apply sum=applyService.get(cond,"getSum");
        renderJson(DataResult.data(sum));
    }
    /**
     * 获取某次招新未处理的人数，没处理一个报名信息则删除，未处理说明deleted=0
     */
    @Param(name = "rId",required = true)
    public void getUntreated(int rId){
        Kv cond=Kv.by("rId",rId);
        Apply sum=applyService.get(cond,"getUntreated");
        renderJson(DataResult.data(sum));
    }




    /**
     * 获取某次招新通过人数
     *
     * @param rId 招新编号
     */
    @Param(name = "rId", required = true)
    public void selectPass(int rId) {
        Kv cond = Kv.by("rId", rId);
        Apply pass = applyService.get(cond, "selectPass");
        renderJson(DataResult.data(pass));
    }

    /**
     * 首页展示最新一次招新通过人员信息
     *
     * @param page     页数
     * @param pageSize 每页的信息条数
     * @param anType  公告类型
     */
    @Clear(AuthInterceptor.class)
    @Param(name = "page", required = true)
    @Param(name = "anType", required = true)
    @Param(name = "pageSize", required = true)
    public void selectSuccessRecruit(int page, int pageSize,int anType) {
        if(anType==2) {
            Page<Record> Recruits = Db.paginate(page, pageSize, "select ap_name,ap_sex,ap_grade,ap_institute,ap_major ", "from apply where ap_pass = 1 and r_id = (SELECT MAX(r_id) from apply)");
            renderJson(DataResult.data(Recruits));
        }
        else {
            renderJson(BaseResult.error404());
        }
    }

    /**
     * 获取某次招新未通过人数
     *
     * @param rId 招新编号
     */
    @Param(name = "rId", required = true)
    public void selectNoPass(int rId) {
        Kv cond = Kv.by("rId", rId);
        Apply noPass = applyService.get(cond, "selectNoPass");
        renderJson(DataResult.data(noPass));
    }


    /**
     * 管理员获取某个报名表的详细信息
     *
     * @param apId 报名表编号
     */
    @Param(name = "apId", required = true)
    public void selectOneApplyById(int apId) {
        Kv cond = Kv.by("apId", apId);
        List<Apply> applyList = applyService.list(cond, "selectOneApply");
        renderJson(DataResult.data(applyList));
    }


    /**
     * 管理员处理报名表（是否通过）
     *
     * @param apId 报名表编号
     * @param pass 是否通过 0未通过，1已通过
     * @throws ParseException
     */
    @Param(name = "apId", required = true)
    @Param(name = "pass", required = true)
    public void passApply(int apId, int pass) throws ParseException {
        Kv cond = Kv.by("apId", apId);
        Apply apply = applyService.get(cond, "selectOneApply");
        apply.setApPass(pass);//pass是否通过，0未通过，1已通过
        apply.setApDeleted(1);//每处理完一个报名表，则删除
        apply.update();
        if (pass == 1) {//如果用户通过报名,则分配一个账号
            String uPwd = "666666";
            String salt = HashKit.generateSaltForSha256();
            User user1 = new User();
            user1.setUSalt(salt);
            user1.setUPwd(HashKit.sha256(salt+HashKit.md5(HashKit.md5(uPwd))));
            user1.setUPhone(apply.getApPhone());
            user1.setUName(apply.getApName());
            user1.setUIns(apply.getApInstitute());
            user1.setUMajor(apply.getApMajor());
            user1.setUGrade(apply.getApGrade());
            user1.setUSex(apply.getApSex());
            user1.setUEmail(apply.getApEmail());
            SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
            Date uCreate = df.parse(df.format(System.currentTimeMillis()));
            user1.setUCreate(uCreate);
            user1.setUDeleted(0);
            renderJson(user1.save() ? BaseResult.ok("用户通过报名") : BaseResult.fail());
        } else {
            renderJson(BaseResult.fail("用户未通过报名"));
        }
    }

    /**
     * 模糊搜索报名信息
     */
    @Param(name = "content",required = true)
    @Param(name = "page", required = true)
    @Param(name = "pageSize", required = true)
    public void fuzzySearchApply(int page, int pageSize,String content){
        String sql="from apply where (ap_name like '%"+content+"%') or (ap_phone like '%"+content+"%') or (ap_sex like '%"+content+"%') or (ap_email like '%"+content+"%') " +
                "or (ap_grade like '%"+content+"%') or (ap_institute like '%"+content+"%') or (ap_major like '%"+content+"%') or (ap_experience like '%"+content+"%') or (ap_goal like '%"+content+"%')";
        Page<Record> Applys = Db.paginate(page, pageSize, "select * ", sql);
        renderJson(DataResult.data(Applys));
    }


}
